package net.lookyou.log.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggerGroup;
import org.springframework.boot.logging.LoggerGroups;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.boot.logging.log4j2.Log4J2LoggingSystem;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class UpdateLevelController {

    @Resource
    private LoggingSystem loggingSystem;

    @Resource
    public LoggerGroups loggerGroups;

    @GetMapping("/updateLogGroupLevel")
    public String updateLogGroupLevel(String groupName, String level) {

        LoggerGroup group = loggerGroups.get(groupName);
        String beforeLevel = group.getConfiguredLevel().name();
        group.configureLogLevel(LogLevel.valueOf(level), loggingSystem::setLogLevel);
        String afterLevel = group.getConfiguredLevel().name();
        return String.format("分组：%s, 更新前级别：%s, 更新后级别：%s", groupName, beforeLevel, afterLevel);

    }
}
